package 
{
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.geom.Point;
	
	/**
	 * ...
	 * @author Lorenzo Nuvoletta
	 */
	public class Main extends Sprite 
	{		
		private var _animators:Vector.<MovieBMPD>;
		private var _mainBMP:Bitmap;
		private var _mainBMPD:BitmapData;
		
		public function Main():void 
		{
			if (stage) init();
			else addEventListener(Event.ADDED_TO_STAGE, init);
		}
		
		private function init(e:Event = null):void 
		{
			removeEventListener(Event.ADDED_TO_STAGE, init);
			// entry point						
			
			_mainBMPD = new BitmapData(stage.stageWidth, stage.stageHeight, false, 0xFFFFFF);
			_mainBMP = new Bitmap();
			_mainBMP.bitmapData = _mainBMPD;
			addChild(_mainBMP);			
			
			createBMPDs();
			addEventListener(Event.ENTER_FRAME, onFrame);
			addChild( new Stats() );
		}
		
		private function createBMPDs():void 
		{
			_animators = new Vector.<MovieBMPD>();
			var n:int = 1;
			for (var i:int = 0; i < n; i++) {
				_animators.push( new MovieBMPD( new DummyBoyRunning() ) );
				_animators[i].position.x = Math.random()*800;
				_animators[i].position.y = ((300/n)*i)+320;			
			}
		}
		
		private function onFrame(e:Event):void 
		{			
			_mainBMPD.fillRect(_mainBMPD.rect, 0xFFFFFF);
			for (var i:int = 0; i < _animators.length; i++) {
				
				_animators[i].position.x += 10 * _animators[i].scaleX;
				if (_animators[i].position.x > 800) _animators[i].scaleX = -1;
				if (_animators[i].position.x < 100) _animators[i].scaleX = 1;
				//_animators[i].rotation++;
				
				_animators[i].nextFrame();
				_animators[i].drawTo(_mainBMPD, true);
			}			
		}
		
		
	}
	
}